package com.wr.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.wr.common.entity.TbSysUser;
import com.wr.common.utils.CookieUtils;
import com.wr.service.LoginService;
import com.wr.service.consumer.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
public class LoginController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private LoginService loginService;

    /**
     *
     * @param url 请求时候的网址
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/login")
    public String login(@RequestParam(required = false)String url, HttpServletRequest request,Model model){
        String token = CookieUtils.getCookieValue(request, "token");

        // token 不为空可能已登录
        if (StrUtil.isNotBlank(token)){
            String loginCode = redisService.get(token);
            if (StrUtil.isNotBlank(loginCode)){
                String json = redisService.get(loginCode);
                if (StrUtil.isNotBlank(json)){
                    TbSysUser tbSysUser = JSONArray.parseObject(json, TbSysUser.class);
                    // 已登录
                    if (tbSysUser!=null){
                        if (StrUtil.isNotBlank(url)){
                            return "redirect:"+url;
                        }

                        // 将登录信息传到前端登录页
                        model.addAttribute("tbSysUser",tbSysUser);
                    }
                }
            }
        }
        if (StrUtil.isNotBlank(url)){
            model.addAttribute("url",url);
        }

        return "login";
    }

    /**
     *
     * @param loginCode 账号
     * @param password 密码
     * @param url 请求时候的网址
     * @param redirectAttributes 重定向携带参数
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/login")
    public String login(String loginCode, String password, @RequestParam(required = false) String url, RedirectAttributes redirectAttributes, HttpServletRequest request, HttpServletResponse response){
        TbSysUser tbSysUser = loginService.login(loginCode, password);
//        // 登录成功
//        if (tbSysUser != null){
//            String token = UUID.randomUUID().toString();
//            String result = redisService.put(token, loginCode, 60 * 60 * 24);
//            if ("ok".equals(result)){
//                CookieUtils.setCookie(request,response,"token",token);
//                return "redirect:"+url;
//            }
//        }
//        return "login";
        // 登录失败
        if(tbSysUser == null){
            redirectAttributes.addFlashAttribute("message","用户名或密码错误，请重新输入！");
        }else {
            String token = UUID.randomUUID().toString();
            String result = redisService.put(token, loginCode, 60 * 60 * 24);
            if (StrUtil.isNotBlank(result) && "ok".equals(result)) {
                CookieUtils.setCookie(request, response, "token", token);
                if (StrUtil.isNotBlank(url)){
                    return "redirect:" + url;
                }
            }
            //熔断处理
            else {
                redirectAttributes.addFlashAttribute("message","服务器异常，请稍后再试");
            }
        }
        return "redirect:/login";
    }


    /**
     * 单点注销
     * @param url
     * @param request
     * @param response
     * @param model
     * @return
     */
    @GetMapping("/loginOut")
    public String loginOut(@RequestParam(required = false) String url,HttpServletRequest request,HttpServletResponse response,Model model){
        CookieUtils.deleteCookie(request,response,"token");
        return login(url,request,model);
    }
}
